<?php
	
	session_start();
	if(array_key_exists("user", $_SESSION)){
		header("redirect.php");
		exit;
	}
	
	
	if(array_key_exists("email", $_POST)){
		include_once("../class/db.php");
		$db = new db();
		
		$email = preg_replace("/[^a-zA-Z0-9_\-\.\s]/", "", $_POST['email']);
		$sql = "SELECT fname, lname FROM users WHERE email = '".$email."'";
		$credentials = $db->execute($sql, "onerow");
		
		if($credentials == false){
			$message = "The email ".$email." doesn't exists on our database, please verify";
		} else {
			
			//Random password generator
			$random_possibilities = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
			$password_clean = "";
			for($i = 0; $i < 8; $i++ ){
				$password_clean .= $random_possibilities[rand(1, strlen($random_possibilities) - 1)];
			}
			
			$password = sha1($password_clean);
			$sql = "UPDATE users SET password = '".$password."' WHERE email = '".$email."'";
			$db->execute($sql, "none");
			
			//Send the email with the information
			$mensaje = '<table width="750" border="0" align="center" cellpadding="0" cellspacing="0">
			  <tr>
				<td><h1>Password Recover Email</h1></td>
			  </tr>
			  <tr>
				<td><p>Hi <strong>'.$credentials['fname'].' '.$credentials['lname'].'</strong>;</p>
				  <p>Recently you request a password recover to access to our system, here it is. Please for security reasons, change your password onces you enter to pie.edu.</p>
				  <p><strong>password:</strong> '.$password_clean.'</p>
				  <p>If you have any problems, remember we have a support section where you can find all the help and instructions you may need to access, use and learn in our platform. We\'ll always be ready to help you with any question or doubt you can have, just contact us.</p><h2>Some helpful links:
				  </h2>
				  <p><strong>Support Center</strong><br />
				  <a href="http://www.pie.edu/help/main.php" target="_blank">http://www.pie.edu/help/main.php</a></p>
				  <p><strong>Questions or comments</strong><br />
				  <a href="http://www.pie.edu/common/contact-us.php" target="_blank">http://www.pie.edu/common/contact-us.php</a></p>
				  <p><strong>Site home</strong><br />
				  <a href="http://www.pie.edu/" target="_blank">http://www.pie.edu/</a></p>
				  <p>Recover Password<br />
				  <a href="http://www.pie.edu/recover.php" target="_blank">http://www.pie.edu/recover.php</a></p></td>
			  </tr>
			</table>';
		
			//HTML Headers
			$cabeceras  = 'MIME-Version: 1.0' . "\r\n";
			$cabeceras .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		
			//Additional Headers
			$cabeceras .= 'From: Password Recover System <noreply@pie.edu>' . "\r\n";
		
			// Mail it
			mail($email, "Password Recover System", $mensaje, $cabeceras);
			
			$email_sent = true;
		}
	}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PIE EDUCATION | Log In</title>
<link rel="stylesheet" type="text/css" href="/css/style-student.css" />
<script type="text/javascript" src="/js/index.js"></script>
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-35230372-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
</head>

<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td bgcolor="#000000"><table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td width="350" height="30" valign="bottom">&nbsp;</td>
        <td align="right" valign="bottom"><a href="/common/about.php" class="top-menu" style="color:#999">ABOUT</a> <a href="/common/contact-us.php" class="top-menu" style="color:#999">CONTACT US</a> <a href="/common/jobs.php" class="top-menu" style="color:#999">JOBS</a> <a href="/login.php" class="top-menu" style="color:#999">SIGN IN</a></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="30" style="background-image:url(/images/top-bg-student.png); background-repeat:repeat-x">&nbsp;</td>
  </tr>
  <tr>
    <td height="550" align="center" valign="top"><table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
      <?php
	if(isset($email_sent)){
	echo '<tr>
        <td valign="top"><h1>Email Sent</h1><div style="font-size:14px; padding-bottom:20px;">Congratulations!! an email has been sent with your loggin information to your email account. The next steps are to go to your email account and see the email, look on the spam folder, sometimes the email system mark it as spam. Inside the email you will find your password, please change your password after you log into the system, that is just a temporary password.</div></td>
      </tr>';
	} else {
	echo '<tr>
        <td valign="top"><h1>Recover your password</h1><div style="font-size:14px; padding-bottom:20px;">To recover your password just type it on the password recover form below, we\'ll send your login information to your email account. Once you get the new password, don\'t forget to change it often to increase your account security</div></td>
      </tr>
      <tr>
        <td><form id="recoverForm" name="recoverForm" method="post" action="recover.php" onsubmit="return recoverValidation();">
          <table width="320" border="0" align="center" cellpadding="0" cellspacing="0">';
		  if(isset($message)){
            echo '<tr>
              <td style="font-size:14px;">'.$message.'</td>
            </tr>';
		  }
			echo '<tr>
              <td><label for="email">Type your email</label>
                <input name="email" type="text" class="form" id="email" tabindex="1" /></td>
            </tr>
            <tr>
              <td><input name="button" type="submit" class="submit" id="button" value="Recover your email" /></td>
            </tr>
          </table>
        </form></td>
        </tr>';
	}
        ?>
    </table></td>
  </tr>
</table>
<div id="bottom-div" align="center">PIE.EDU All Rights Reserved 2012<br />
      <a href="/common/privacy.php" class="bottom-menu" style="color:#999">Privacy Policy</a> | <a href="/common/terms.php" class="bottom-menu"  style="color:#999">Terms of Use</a> | <a href="/help/main.php" class="bottom-menu" style="color:#999">Help Center</a> | <a href="/common/jobs.php" class="bottom-menu" style="color:#999">Jobs</a></div>
</body>
</html>